﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dev.Control;
using SfcorpDLL.biz;
using SfcorpDLL.ety;
using TT.Common;
using SQLHelper;
using TT.Module.sysLogin;
//using TT.ORM;
using DevExpress.XtraBars;

namespace Sfcorp.form
{
    public partial class frmCustomer : BaseFormDev
    {
        bizCustomer bzCustomer;

        public frmCustomer()
        {
            InitializeComponent();
        }
        private string formSta;
        public string FormSta
        {
            set
            {
                formSta = value;
                switch (formSta)
                {
                    case "A":
                        lblBar.Caption = "新建状态";
                        break;
                    case "E":
                        lblBar.Caption = "编辑状态";
                        break;
                    default:
                        break;
                }
            }
            get
            {
                return formSta;
            }
        }
        //显示行号
        private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                }
                else if (e.RowHandle < 0 && e.RowHandle > -1000)
                {
                    e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
                    e.Info.DisplayText = "G" + e.RowHandle.ToString();
                }
            }
        }

        public override void InitForm()
        {
            #region 权限处理
            TT.ORM.etyUsersModCtrl ey = new TT.ORM.etyUsersModCtrl();
            ey.USER_ID = global.basSysPara.UserID;
            ey.MOD_ID = this.MOD_ID;
            global.bzUser.SetFormSecrity(this, barManager1.Items, ey);

            //btnRefresh.ItemClick += new ItemClickEventHandler(btnRefresh_ItemClick);
            //btnSave.ItemClick += new ItemClickEventHandler(btnSave_ItemClick);
            //btnNew.ItemClick += new ItemClickEventHandler(btnNew_ItemClick);
            //btnDelete.ItemClick += new ItemClickEventHandler(btnDelete_ItemClick);
            btnSec.ItemClick += new ItemClickEventHandler(btnSec_ItemClick);
            #endregion

            base.InitForm();
        }

        void btnSec_ItemClick(object sender, ItemClickEventArgs e)
        {
            TT.HelpInfo.MsgBox.ShowSuccessFailBox(
            global.bzUser.CreateFormSecrity(this, barManager1.Items, global.basSysPara.UserID, this.MOD_ID)
            );
        }

        private void frmCustomer_Load(object sender, EventArgs e)
        {
            try
            {
                //刷新界面所需要的数据
                TT.Module.RefreshTable.BaseTableRefreshByTables("CODE_REF,C_I_E,C_COLLABORATE,C_FEE_WAY,C_FEE_MOD", false);

                bzCustomer = new bizCustomer(global.basSysPara);
                #region 下拉数据
                cboFee_Way_Id.DataSource = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(global.ilFeeWay);
                cboEnable_Flag.DataSource = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(global.ilCustomerEnabled);
                cboCollaborate_Id.DataSource = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(global.ilCollaboRate);
                cboFrtagygroup_I_E_Id.DataSource = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(global.ilCIE);
                cboFee_Mod_Cod.DataSource = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(global.ilFeeMod);
                #endregion

                #region 表格数据
                ggcCustomer.GridView = this.gridView1;
                ggcCustomer.GridView.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.Click;
                string sMapingCols = "CUSTOMER_ID,CUSTOMER_NAM,ENABLE_FLAG,REC_NAM,REC_TIM,SEQ_NO,REMARKS,IMP_AMOUNT_SHOW_FLAG,EXP_AMOUNT_SHOW_FLAG,FEE_WAY_NAM,COLLABORATE_NAM,I_E_NAM,FEE_MOD_NAM";
                string sMapingHeaderTxt = "客户代码,客户名称,生效状态,录入者,录入时间,排列顺序,备注,进口显示金额,出口显示金额,结算方式,合作方,办单类型,费付方式";
                ggcCustomer.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt);
                this.winUtil.LoadGridLayout(ggcCustomer);

                RefreshDate();
                #endregion
                FormSta = "A";
                ClearForm();
                bar3.Text = "状态栏";
            }
            catch (Exception ex)
            {
            }
        }

        #region 事件
        //保存
        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveDate();
        }

        private void btnSearch_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.gridView1.OptionsFind.AlwaysVisible = !this.gridView1.OptionsFind.AlwaysVisible;
        }

        private void ggcCustomer_Click(object sender, EventArgs e)
        {
            etyCustomer ey = srcBinding.Current as etyCustomer;
            if (ey == null)
                return;
            ClearForm();
            TT.Common.formProcess.SetFormEntity(ey, grpDtl);
            cboFee_Way_Id.GetCodeText();
            cboEnable_Flag.GetCodeText();
            cboCollaborate_Id.GetCodeText();
            cboFrtagygroup_I_E_Id.GetCodeText();
            cboFee_Mod_Cod.GetCodeText();
            FormSta = "E";
        }

        private void btnExit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            F12();
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DeleteDate();
        }

        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            NewData();
        }

        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RefreshDate();
        }

        public override Dev.Control.etyQuickKey RefreshDate()
        {
            srcBinding.DataSource = bzCustomer.GetCustomerDtl(new etyCustomer());
            ggcCustomer.DataSource = srcBinding;
            ggcCustomer.AutoColumnWidth();
            return base.RefreshDate();
        }

        public override etyQuickKey SaveDate()
        {
            //etyCustomer eyGrid = srcBinding.Current as etyCustomer;
            etyCustomer eyCust = new etyCustomer();
            formProcess.GetFormEntity(eyCust, grpDtl);
            //eyCust.ROW_ID = eyGrid.ROW_ID;
            if (check_data(eyCust))
            {
                daoResult rst;

                if (FormSta == "A")
                    rst = bzCustomer.Insert_Customer(eyCust);
                else
                    rst = bzCustomer.Update_Customer(eyCust);
                if (rst.Exec_Success)
                {
                    TT.HelpInfo.MsgBox.ShowSuccessBox("保存成功。");
                    RefreshDate();
                }
                else
                {
                    TT.HelpInfo.MsgBox.ShowWarnBox(rst.Error_String);
                    formProcess.SetControlFocus(rst.Focus_Control, grpDtl);
                }
            }
            return base.SaveDate();
        }

        public override etyQuickKey DeleteDate()
        {
            etyCustomer ey = srcBinding.Current as etyCustomer;
            if (ey == null)
                return null;
            if (TT.HelpInfo.MsgBox.ShowQuestionBox(string.Format("是否删除客户：{0}？", new string[] { ey.CUSTOMER_NAM })))
            {
                daoResult rst = bzCustomer.Delete_Customer(ey);
                if (rst.Exec_Success)
                {
                    TT.HelpInfo.MsgBox.ShowSuccessBox("删除成功。");
                    lblBar.Caption = "";
                    RefreshDate();
                }
                else
                {
                    TT.HelpInfo.MsgBox.ShowWarnBox(rst.Error_String);
                    formProcess.SetControlFocus(rst.Focus_Control, grpDtl);
                }
            }
            return base.DeleteDate();
        }

        public override etyQuickKey NewData()
        {
            FormSta = "A";
            etyCustomer ey = srcBinding.Current as etyCustomer;
            if (ey == null)
                return null;

            TT.Common.formProcess.SetFormEntity(ey, grpDtl);
            ClearForm();

            return base.NewData();
        }

        private void ClearForm()
        {
            txtCustomer_Id.Text = "";
            txtCustomer_Nam.Text = "";
            cboFee_Way_Id.SelectedIndex = -1; 
            cboEnable_Flag.SelectedIndex = 1;
            cboCollaborate_Id.SelectedIndex = 0;
            cboFrtagygroup_I_E_Id.SelectedIndex = 0;
            cboFee_Mod_Cod.SelectedIndex = -1;
            txtSeq_No.Text = "";
            cboCollaborate_Id.GetCodeText();
            cboEnable_Flag.GetCodeText();
        }

        #endregion

        #region 函数
        private bool check_data(etyCustomer ey)
        {
            if (string.IsNullOrEmpty(ey.CUSTOMER_ID.Trim()))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("客户ID号不能为空");
                txtCustomer_Id.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(ey.CUSTOMER_NAM.Trim()))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("客户名称不能为空");
                txtCustomer_Nam.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(ey.ENABLE_FLAG.Trim()))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("生效状态不能为空");
                cboEnable_Flag.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(ey.COLLABORATE_ID.Trim()))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("合作方不能为空");
                cboCollaborate_Id.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(ey.FRTAGYGROUP_I_E_ID.Trim()))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("办单类型不能为空");
                cboFrtagygroup_I_E_Id.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(ey.FEE_WAY_ID.Trim()))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("结算方式不能为空");
                cboFee_Way_Id.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(ey.FEE_MOD_COD.Trim()))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("付费方式不能为空");
                cboFee_Mod_Cod.Focus();
                return false;
            }
            return true;
        }
        #endregion
         

    }
}
